package com.yang.Offer;

public class Offer04 {
    public static void main(String[] args) {
        SolutionOffer04 solutionOffer04 = new SolutionOffer04();
        int[][] nums = {
                {1,   4,  7, 11, 15},
                {2,   5,  8, 12, 19},
                {3,   6,  9, 16, 22},
        {10, 13, 14, 17, 24},
    {18, 21, 23, 26, 30} };

        boolean numberIn2DArray = solutionOffer04.findNumberIn2DArray(nums, 20);
        System.out.println(numberIn2DArray);
    }
}

class SolutionOffer04 {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        int i = matrix.length-1, j = 0;
        // 注意边界索引限制
        while (i >= 0 && j < matrix[0].length)
        {
            if(target > matrix[i][j]){
                j++;
            }else if(target < matrix[i][j]){
                i--;
            // 如果都不满足 那么在数组中就不存在这个数字
            }else {
                return true;
            }
        }
        return false;
    }
}
